// 格式化日期
function formatDate(date) {
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()
  return [year, month, day].map(formatNumber).join('-')
}

// 日期 +天数
function addDate(date, days) {
  var d = new Date(date);
  d.setDate(d.getDate() + days);
  var month = d.getMonth() + 1;
  var day = d.getDate();
  if (month < 10) {
    month = "0" + month;
  }
  if (day < 10) {
    day = "0" + day;
  }
  var val = d.getFullYear() + "-" + month + "-" + day;
  return val;
}
// 日期格式化
function formatTime(val) {
  if (!val) return "";

  var re = /-?\d+/;
  var m = re.exec(val);
  var d = new Date(parseInt(m[0]));
  // 按【2012-02-13 09:09:09】的格式返回日期    
  return d.format("yyyy-MM-dd hh:mm:ss");
}
// 日期格式化
function formatTimebytype(val, datetype) {
  if (!val) return "";
  var a = val.split("T");
  var b = a[0].split("-");
  var c = a[1].split(":");
  var date = new Date(b[0], b[1] - 1, b[2], c[0], c[1], c[2]);
  return date.format(datetype);
}
// 用于格式化日期显示
Date.prototype.format = function (format) //author: meizz 
{
  var o = {
    "M+": this.getMonth() + 1, //month         
    "d+": this.getDate(), //day         
    "h+": this.getHours(), //hour         
    "m+": this.getMinutes(), //minute         
    "s+": this.getSeconds(), //second         
    "q+": Math.floor((this.getMonth() + 3) / 3), //quarter         
    "S": this.getMilliseconds() //millisecond     
  }
  if (/(y+)/.test(format))
    format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
  for (var k in o)
    if (new RegExp("(" + k + ")").test(format))
      format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
  return format;
}

function formatNumber(n) {
  n = n.toString()
  return n[1] ? n : '0' + n
}

function getWeek(str) {

  // 拆分日期为年 月 日
  var YEAR = str.substring(0, 4),
    MONTH = str.substring(5, 7),
    DATE = str.slice(-2);

  // 拼接为 2016/10/02 可用于请求日期格式
  var dateDay = YEAR + "/" + MONTH + "/" + DATE;

  // 获取周几
  var week = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'],
    day = new Date(dateDay).getDay();

  // return {
  //    // "dateDay": MONTH + "月" + DATE + "日 " + week[day]
  //    "dateDay": week[day]
  // }
  return week[day];

}

function addDayCount(AddDayCount) {
  var dd = new Date();
  dd.setDate(dd.getDate() + AddDayCount); //获取AddDayCount天后的日期    
  var y = dd.getFullYear();
  var m = (dd.getMonth() + 1) < 10 ? "0" + (dd.getMonth() + 1) : (dd.getMonth() + 1); //获取当前月份的日期，不足10补0    
  var d = dd.getDate() < 10 ? "0" + dd.getDate() : dd.getDate(); //获取当前几号，不足10补0    
  return y + "-" + m + "-" + d;
}

//计算时间差-分钟
function TimeDifference(time1, time2) {
  //判断开始时间是否大于结束日期
  if (time1 > time2) {
    console.log("开始时间不能大于结束时间！");
    return false;
  }

  //截取字符串
  var begin1 = time1.substr(0, 10).split("-");
  var end1 = time2.substr(0, 10).split("-");

  //将拆分的数组重新组合，并实例成化新的日期对象
  var date1 = new Date(begin1[0] + - +begin1[1] + - +begin1[2]);
  var date2 = new Date(end1[0] + - +end1[1] + - +end1[2]);

  //得到两个日期之间的差值m，以分钟为单位
  var m = parseInt(Math.abs(date2 - date1) / 1000 / 60);

  //小时数和分钟数相加得到总的分钟数
  //time1.substr(11,2)截取字符串得到时间的小时数
  //parseInt(time1.substr(11,2))*60把小时数转化成为分钟
  var min1 = parseInt(time1.substr(11, 2)) * 60 + parseInt(time1.substr(14, 2));
  var min2 = parseInt(time2.substr(11, 2)) * 60 + parseInt(time2.substr(14, 2));

  //两个分钟数相减得到时间部分的差值，以分钟为单位
  var n = min2 - min1;

  var sec1 = parseInt(time1.substr(17, 2)) / 60;
  var sec2 = parseInt(time2.substr(17, 2)) / 60;
  var s = sec2 - sec1;

  //将日期和时间两个部分计算出来的差值相加，即得到两个时间相减后的分钟数
  var minutes = m + n + s;
  return minutes.toFixed(2);
}

// 通用JSON数组去重
function uniqueArray(array, key) {
  var result = [array[0]];
  for (var i = 1; i < array.length; i++) {
    var item = array[i];
    var repeat = false;
    for (var j = 0; j < result.length; j++) {
      if (item[key] == result[j][key]) {
        repeat = true;
        break;
      }
    }
    if (!repeat) {
      result.push(item);
    }
  }
  return array;
}

module.exports = {
  uniqueArray: uniqueArray,
  TimeDifference: TimeDifference,
  addDayCount: addDayCount,
  formatTime: formatTime,
  addDate: addDate,
  formatDate: formatDate,
  formatTimebytype: formatTimebytype,
  getWeek: getWeek
}